///index.js
//获取应用实例
const app = getApp();
let pNo = 1, gId = '', goodsId = '', busid='';
const getToken= require('../../utils/get_token.js');
const formatTime = require('../../utils/util.js');

let  openId = '', memberId = '';
let cookie, token;
function getRandomColor() {
  const rgb = []
  for (let i = 0; i < 3; ++i) {
    let color = Math.floor(Math.random() * 256).toString(16)
    color = color.length == 1 ? '0' + color : color
    rgb.push(color)
  }
  return '#' + rgb.join('')
};
const ckToken = async (that) => {//token确认
  let nTime = Math.floor(new Date().getTime() / 1000);
  let oldTime = wx.getStorageSync('ntime') || 0;
  if (nTime < oldTime)//现在的时间小于到期时间
  {
    thou(that);
  } else {
    let tokens = await getToken.getToken();
    thou(that);
  }
}
const thou = (that) => {//token没有失效时执行的代码
  detail(that);
  giveLike(that);
  secure(that);
  member(that);
}
const detail = (that) => {//全时播详情
  let params = ['shop/allTimePlay/getAllTimePlayGoodsDetail', {"id":gId,"goodsId": goodsId,"memberId": memberId}]
  getToken.getData(function (res) {
    let data = res.data;
    data.intPrice = (Number(data.intPrice) / 100).toFixed(2);
    data.goodTimes = Number(data.goodTimes);
    that.data.poster = 'https://www.globalhexi.com/' + data.image;
    that.data.src = data.url;
    if (data.playStatus == '3') {
      that.data.nShow = true;
    }
    that.setData({
      ts: data,
      goodTimes: data.goodTimes,
      poster: that.data.poster,
      nShow: that.data.nShow,
      src: that.data.src
    })
  }, ...params)
};
const giveLike = (that) => {//点赞列表
  let params = ['shop/allTimePlay/getPlayGoodsRecordList', {"id":gId,"goodsId":goodsId,"memberId": memberId}]
  getToken.getData(function (res) {
    let data = res.data;
    console.log(data)
    for (let i = 0; i < data.rows.length; i++) {
      data.rows[i].text = data.rows[i].username + '点赞了';
      data.rows[i].color = getRandomColor();
      data.rows[i].time = i + 1;
    }
    that.setData({
      danmuList: data.rows
    })
  }, ...params)
};
const secure = (that) => {//商品列表
  let params = ['shop/allTimePlay/getBusbasePlayGoodsList', {"pageNo":1,"memberId": memberId,"busbaseId":busid}]
  getToken.getData(function (res) {
    let data = res.data;
    console.log(data)
    if (data.rows.length > 0) {
      for (let i = 0; i < data.rows.length; i++) {
        //data.rows[i].goodsImageStore = data.rows[i].goodsImageStore.substring(1, data.rows[i].goodsImageStore.length - 1);
        data.rows[i].goodsImageStore = JSON.parse(data.rows[i].goodsImageStore);
        data.rows[i].img = 'https://www.globalhexi.com/' + data.rows[i].goodsImageStore[0].path + '/small/' + data.rows[i].goodsImageStore[0].id + '.jpg';
        data.rows[i].intPrice = (data.rows[i].intPrice / 100).toFixed(2);
      }
      that.setData({
        qList: data.rows,
        qlength: data.rows.length,
        shop: data.rows[0].shopName,
        slogo: data.rows[0].shopLogo
      })
    }
  }, ...params)
};
const member = (that) => {
  let params = ['common/member/member', {"id":memberId}]
  getToken.getData(function (res) {
    that.data.username = res.data.username;
  }, ...params)
}
Page({
  data: {
    hidden: false,
    boolT: true,
    fShow:false,
    lShow:false,
    tShow:true,
    vShow:false,
    nShow:false,
    username:'',
    qList: [],
    danmuList: [],
    ts:{},
    goodTimes:0,
    qlength:0,
    shop:'',
    slogo:'',
    poster:'',
    src:''
  },
  onLoad: function (options) {//http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400
    let that = this;
    gId=options.id;
    goodsId = options.gd;
    busid = options.bd;
    memberId = wx.getStorageSync('memberId') || '';
    openId = wx.getStorageSync('openId') || '';
    cookie = wx.getStorageSync('cookie') || '';
    token = wx.getStorageSync('token') || '';
    that.videoContext = wx.createVideoContext('myVideo')
    ckToken(that);
  },
  clike: function (e) {//第一次点赞
    let that = this;
    if (that.data.boolT) {
      that.data.boolT = false;
      let params = ['shop/allTimePlay/addPlayRecord', {"id": gId,"goodsId": goodsId ,"memberId":memberId,"content":"","playStatus":"1"}]
      getToken.getData(function (res) {
        let data = res.data;
        if (data.flag) {
          that.data.goodTimes = that.data.goodTimes + 1;
          that.setData({
            fShow: true,
            tShow: false,
            goodTimes: that.data.goodTimes
          })
          that.videoContext.sendDanmu({
            text: that.data.username + '点赞了',
            color: getRandomColor()
          })
        }
        setTimeout(function () { that.data.boolT = true; }, 500)
      }, ...params)
    }
  },
  clikev: function (e) {//第二次点赞
    let that = this;
    if (that.data.boolT) {
      that.data.boolT = false;
      let params = ['shop/allTimePlay/addPlayRecord', {"id": gId,"goodsId": goodsId,"memberId":memberId,"content":"","playStatus":"1"}]
      getToken.getData(function (res) {
        let data = res.data;
        if (data.flag) {
          that.data.goodTimes = that.data.goodTimes + 1;
          that.setData({
            vShow: true,
            goodTimes: that.data.goodTimes
          })
          that.videoContext.sendDanmu({
            text: that.data.username + '点赞了',
            color: getRandomColor()
          })
        }
        setTimeout(function () { that.data.boolT = true; }, 500)
        setTimeout(function () {
          that.setData({
            vShow: false
          })
        }, 5000)
      }, ...params)
    }
  },
  show_list:function(e){//商品展示切换
  let that=this;
  that.setData({
    lShow: !that.data.lShow
  })
  },
  go_detail: function (e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    if (that.data.boolT) {
      that.data.boolT = false;
      let params = [ 'shop/goods/goods', { "goodId":id}]
      getToken.getData(function (res) {
        if (res.data.goodsPinTuanId == '') {
          wx.navigateTo({
            url: '../detail/detail?id=' + id,
            success: function () {
              setTimeout(function () { that.data.boolT = true; }, 500)
            }
          })
        }
        else {
          wx.navigateTo({
            url: '../paired_detail/paired_detail?scene=' + res.data.goodsPinTuanId,
            success: function () {
              setTimeout(function () { that.data.boolT = true; }, 500)
            }
          })
        }
      }, ...params)
    }
  },
  error:function(e){//播放出错
   let that=this;
   that.setData({
     nShow:true
   })
  }

})
